/**
 * The Community Project
 * Copyright Team1 Productions
 * 
 * Joshua Amick
 * Stephen Cunningham
 * Thema Davis
 * Tomika Dawson
 * Lucie DeCristofaro
 * 
 * All Rights Reserved.
 */

package com.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.domain.Email;
import com.domain.User;
import com.forms.SendGroupEmailForm;

public class SaveEmailRequestAction extends CommunityBaseAction{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		super.execute(mapping, form, request, response);
		ActionForward forward;
		// Cast the actionForm, if there is one.
		//
		SendGroupEmailForm f = (SendGroupEmailForm) form;
		
		//Check for subject
		if(f.getSubject()!=null&&f.getSubject().length()>0){
			
			//Check user
			User user = (User)request.getSession().getAttribute("AUTH_USER");
			if(user!=null){
				boolean toAll = true;
				//if(f.getToAll().equals("Admin"))toAll = false;
				Email email = this.biz.getPrivateMgr().saveEmail(user,f.getSubject(),f.getBody(),f.getReplyToAddress(),toAll,false);
				if(email != null && email.getKeyId()>0){
					request.setAttribute("INFO", "Email successfully submitted for processing.");
					forward = mapping.findForward("success");
				}
				else{
					request.setAttribute("ERROR", "Error occurred while saving.");
					forward = mapping.findForward("failure");
				}
			}
			else{
				request.setAttribute("ERROR", "Authentication error.");
				forward = mapping.findForward("failure");
			}
		}
		else{
			request.setAttribute("ERROR", "Subject is required");
			forward = mapping.findForward("failure");
		}
		request.setAttribute("FORM", f);
		// Set request attributes
		
		// return success

		return forward;
	}
}
